package it.asvp.asvp_portal.data.dao.impl;

import it.asvp.asvp_portal.data.dao.NotificationDao;
import it.asvp.asvp_portal.data.entity.notification.Notification;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class NotificationDaoImpl implements NotificationDao{
   
	private static final Log log = LogFactory.getLog(NotificationDaoImpl.class);

	@Autowired
	SessionFactory sessionFactory;

	public Long persist(Notification transientInstance) {
		log.debug("persisting Notification instance");
		Long id;
		
		try {
			id=(Long)sessionFactory.getCurrentSession().save(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
		return id;
	}

	public void delete(Notification persistentInstance) {
		log.debug("deleting Notification instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Notification merge(Notification detachedInstance) {
		log.debug("merging Notification instance");
		try {
			Notification result = (Notification) sessionFactory
					.getCurrentSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Notification findById(java.lang.Long id) {
		log.debug("getting Notification instance with id: " + id);
		try {
			Notification instance = (Notification) sessionFactory
					.getCurrentSession()
					.get("it.asvp.asvp_portal.data.dao.generator.Notification",
							id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Notification> findAll() {
		log.debug("getting all Notification instances:");
		
		Criteria cr=sessionFactory.getCurrentSession().createCriteria(Notification.class);
		List<Notification> list= (List<Notification>)cr.list();
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<Notification> findByStartDate(Date date){
        log.debug("getting all Notification by StartDate");
		
         Criteria cr=sessionFactory.getCurrentSession().createCriteria(Notification.class);
		 cr.add(Restrictions.eq("startDate",date));
		 List<Notification> list = (List<Notification>)cr.list();
		 return list;  
	}

}
